<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简洁文件夹风格树形菜单</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1a2980, #26d0ce);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            color: #333;
        }
        
        .container {
            width: 100%;
            max-width: 1200px;
            display: flex;
            gap: 30px;
            flex-wrap: wrap;
        }
        
        .card {
            background: rgba(255, 255, 255, 0.97);
            border-radius: 16px;
            box-shadow: 0 12px 35px rgba(0, 0, 0, 0.25);
            padding: 30px;
            flex: 1;
            min-width: 300px;
            transition: transform 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        
        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, #4e54c8, #8f94fb);
        }
        
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
        }
        
        .card-header {
            margin-bottom: 20px;
            padding-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 12px;
        }
        
        .card-header i {
            font-size: 28px;
            color: #4e54c8;
            background: rgba(78, 84, 200, 0.1);
            width: 50px;
            height: 50px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        h1 {
            color: #2c3e50;
            font-size: 32px;
            margin-bottom: 5px;
            font-weight: 700;
        }
        
        .description {
            color: #7f8c8d;
            font-size: 16px;
            margin-bottom: 25px;
            line-height: 1.6;
        }
        
        .toolbar {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            flex-wrap: wrap;
            background: #f8f9fa;
            padding: 15px;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }
        
        .toolbar-btn {
            display: flex;
            align-items: center;
            gap: 8px;
            background: #4e54c8;
            color: white;
            border: none;
            border-radius: 10px;
            padding: 10px 20px;
            cursor: pointer;
            font-weight: 600;
            transition: all 0.3s;
            box-shadow: 0 4px 10px rgba(78, 84, 200, 0.3);
        }
        
        .toolbar-btn:hover {
            background: #3f43a8;
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(78, 84, 200, 0.4);
        }
        
        .toolbar-btn:disabled {
            background: #adb5bd;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        
        .tree-container {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            height: 500px;
            overflow-y: auto;
            padding: 15px;
            border: 1px solid #eaeaea;
        }
        
        .tree-menu {
            list-style: none;
            padding-left: 0;
        }
        
        .tree-node {
            margin: 8px 0;
            position: relative;
            transition: all 0.2s;
        }
        
        .node-content {
            display: flex;
            align-items: center;
            padding: 12px 15px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.2s;
            background: #f8f9fa;
            position: relative;
            z-index: 2;
            border: 1px solid #e9ecef;
        }
        
        .node-content:hover {
            background: #e9ecef;
            transform: translateX(5px);
        }
        
        .node-content.selected {
            background: #4e54c8;
            color: white;
            box-shadow: 0 4px 12px rgba(78, 84, 200, 0.3);
            border-color: #4e54c8;
            transform: translateX(8px);
        }
        
        .node-content.selected .node-icon {
            color: white;
        }
        
        .node-icon {
            margin-right: 10px;
            width: 24px;
            text-align: center;
            font-size: 18px;
            transition: all 0.2s;
        }
        
        .folder-icon {
            color: #ff9800;
        }
        
        .file-icon {
            color: #4e54c8;
        }
        
        .node-label {
            flex: 1;
            font-size: 17px;
            font-weight: 500;
            transition: all 0.2s;
        }
        
        .children {
            padding-left: 35px;
            margin-left: 15px;
            display: none;
            border-left: 2px solid #e0e0e0;
        }
        
        .expanded > .children {
            display: block;
        }
        
        .loading {
            color: #6c757d;
            font-size: 14px;
            padding: 10px 15px;
            display: flex;
            align-items: center;
            gap: 10px;
            font-style: italic;
        }
        
        .spinner {
            border: 2px solid rgba(0, 0, 0, 0.1);
            border-left-color: #4e54c8;
            border-radius: 50%;
            width: 16px;
            height: 16px;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        .instructions {
            margin-top: 25px;
            background: linear-gradient(to right, #e3f2fd, #f0f7ff);
            padding: 20px;
            border-radius: 10px;
            border-left: 4px solid #4e54c8;
        }
        
        .instructions h3 {
            color: #1976d2;
            margin-bottom: 12px;
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 20px;
        }
        
        .instructions ul {
            padding-left: 25px;
        }
        
        .instructions li {
            margin: 10px 0;
            line-height: 1.5;
            font-size: 15px;
        }
        
        .instructions i {
            color: #1976d2;
            margin-right: 8px;
            width: 20px;
            text-align: center;
        }
        
        .feature-card {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 12px;
            margin-top: 20px;
            border: 1px solid #eaeaea;
        }
        
        .feature-card h3 {
            color: #2c3e50;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 22px;
        }
        
        .features {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 15px;
        }
        
        .feature {
            background: white;
            padding: 15px;
            border-radius: 10px;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
            display: flex;
            align-items: center;
            gap: 10px;
            transition: all 0.2s;
            border: 1px solid #f0f0f0;
        }
        
        .feature:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .feature i {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #4e54c8, #8f94fb);
            color: white;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
        }
        
        .feature span {
            font-weight: 600;
            color: #2c3e50;
        }
        
        .selected-panel {
            background: linear-gradient(135deg, #4e54c8, #8f94fb);
            color: white;
            border-radius: 12px;
            padding: 20px;
            margin-top: 20px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .selected-panel h3 {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 22px;
            margin-bottom: 15px;
        }
        
        .selected-panel .info-item {
            margin: 10px 0;
            display: flex;
            align-items: center;
        }
        
        .selected-panel .info-item i {
            width: 30px;
            font-size: 18px;
        }
        
        .selected-panel .info-content {
            font-weight: 500;
            font-size: 17px;
        }
        
        .no-selection {
            text-align: center;
            padding: 20px;
            color: rgba(255, 255, 255, 0.8);
            font-style: italic;
        }
        
        .keyboard-hint {
            background: rgba(255, 255, 255, 0.15);
            padding: 10px 15px;
            border-radius: 8px;
            margin-top: 15px;
            font-size: 14px;
        }
        
        .key {
            display: inline-block;
            background: rgba(0, 0, 0, 0.2);
            padding: 2px 8px;
            border-radius: 4px;
            font-family: monospace;
            margin: 0 3px;
        }
        
        .data-status {
            background: #e8f5e9;
            padding: 10px 15px;
            border-radius: 8px;
            margin-top: 15px;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        @media (max-width: 768px) {
            .container {
                flex-direction: column;
            }
            
            .card {
                width: 100%;
            }
            
            h1 {
                font-size: 26px;
            }
            
            .toolbar {
                flex-direction: column;
            }
            
            .toolbar-btn {
                width: 100%;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <div class="card-header">
                <i class="fas fa-folder-tree"></i>
                <div>
                    <h1>简洁文件夹树形菜单</h1>
                    <p class="description">无小圆点/小方块的简洁设计，支持异步加载</p>
                </div>
            </div>
            
            <!-- 顶部操作栏 -->
            <div class="toolbar">
                <button id="addRootBtn" class="toolbar-btn">
                    <i class="fas fa-plus-circle"></i> 添加根节点
                </button>
                <button id="addChildBtn" class="toolbar-btn" disabled>
                    <i class="fas fa-plus"></i> 添加子节点
                </button>
                <button id="editNodeBtn" class="toolbar-btn" disabled>
                    <i class="fas fa-edit"></i> 编辑节点
                </button>
                <button id="deleteNodeBtn" class="toolbar-btn" disabled>
                    <i class="fas fa-trash-alt"></i> 删除节点
                </button>
                <button id="expandAllBtn" class="toolbar-btn">
                    <i class="fas fa-expand"></i> 全部展开
                </button>
                <button id="collapseAllBtn" class="toolbar-btn">
                    <i class="fas fa-compress"></i> 全部折叠
                </button>
            </div>
            
            <div class="tree-container">
                <ul class="tree-menu" id="treeMenu"></ul>
            </div>
            
            <div class="data-status">
                <i class="fas fa-database" style="color:#4e54c8;"></i>
                <div>当前树包含 <span id="nodeCount">0</span> 个节点，<span id="folderCount">0</span> 个文件夹</div>
            </div>
            
            <div class="instructions">
                <h3><i class="fas fa-info-circle"></i>使用说明</h3>
                <ul>
                    <li><i class="fas fa-folder"></i> 文件夹图标表示节点类型</li>
                    <li><i class="fas fa-folder-open"></i> 展开的文件夹表示已展开节点</li>
                    <li><i class="fas fa-file"></i> 文件图标表示叶子节点</li>
                    <li><i class="fas fa-mouse-pointer"></i> 点击节点文本选中节点（蓝色高亮）</li>
                    <li><i class="fas fa-chevron-right"></i>点击文件夹图标展开/折叠节点</li>
                    <li><i class="fas fa-cloud-download-alt"></i> 带虚线边框的节点支持异步加载</li>
                </ul>
            </div>
        </div>
        
        <div class="card">
            <div class="card-header">
                <i class="fas fa-star"></i>
                <div>
                    <h1>功能与信息</h1>
                    <p class="description">简洁高效的树形菜单组件</p>
                </div>
            </div>
            
            <div class="feature-card">
                <h3><i class="fas fa-bolt"></i>核心功能</h3>
                <div class="features">
                    <div class="feature">
                        <i class="fas fa-folder"></i>
                        <span>文件夹图标</span>
                    </div>
                    <div class="feature">
                        <i class="fas fa-toolbox"></i>
                        <span>顶部操作栏</span>
                    </div>
                    <div class="feature">
                        <i class="fas fa-check-circle"></i>
                        <span>节点选中功能</span>
                    </div>
                    <div class="feature">
                        <i class="fas fa-cloud-download-alt"></i>
                        <span>异步加载</span>
                    </div>
                    <div class="feature">
                        <i class="fas fa-save"></i>
                        <span>状态保存</span>
                    </div>
                    <div class="feature">
                        <i class="fas fa-expand-arrows-alt"></i>
                        <span>展开/折叠</span>
                    </div>
                    <div class="feature">
                        <i class="fas fa-keyboard"></i>
                        <span>键盘导航</span>
                    </div>
                    <div class="feature">
                        <i class="fas fa-sync"></i>
                        <span>批量操作</span>
                    </div>
                </div>
            </div>
            
            <div class="selected-panel">
                <h3><i class="fas fa-info-circle"></i> 当前选中节点</h3>
                <div id="selectedNodeInfo">
                    <div class="no-selection">
                        <i class="fas fa-info-circle"></i> 请从左侧树中选择一个节点
                    </div>
                </div>
            </div>
            
            <div class="keyboard-hint">
                <p><i class="fas fa-keyboard"></i> <strong>键盘快捷键:</strong></p>
                <p><span class="key">↑</span> 向上选择节点</p>
                <p><span class="key">↓</span> 向下选择节点</p>
                <p><span class="key">Enter</span> 添加根节点</p>
                <p><span class="key">+</span> 添加子节点</p>
                <p><span class="key">E</span> 编辑节点</p>
                <p><span class="key">Del</span> 删除节点</p>
            </div>
            
            <div style="margin-top: 20px;">
                <button id="resetBtn" class="toolbar-btn" style="background: #6c757d; width: 100%;">
                    <i class="fas fa-sync-alt"></i> 重置为默认树结构
                </button>
            </div>
        </div>
    </div>

    <script>
        // 树节点类
        class TreeNode {
            constructor(id, name, children = []) {
                this.id = id;
                this.name = name;
                this.children = children;
                this.expanded = false;
                this.loaded = false;
                this.selected = false;
            }
            
            isLeaf() {
                return this.children.length === 0 && this.loaded;
            }
        }

        // 树形菜单类
        class TreeMenu {
            constructor(containerId) {
                this.container = document.getElementById(containerId);
                this.treeData = [];
                this.nextId = 1;
                this.selectedNode = null;
                this.loadTreeState();
                this.render();
                this.setupEventListeners();
                this.setupKeyboardNavigation();
                this.updateToolbarState();
                this.updateDataStatus();
            }
            
            // 加载树状态
            loadTreeState() {
                const savedTree = localStorage.getItem('treeData');
                if (savedTree) {
                    this.treeData = JSON.parse(savedTree);
                    this.calculateNextId();
                } else {
                    this.initializeDefaultTree();
                }
            }
            
            // 保存树状态
            saveTreeState() {
                localStorage.setItem('treeData', JSON.stringify(this.treeData));
                this.updateDataStatus();
            }
            
            // 计算下一个ID
            calculateNextId() {
                let maxId = 0;
                
                const findMaxId = (nodes) => {
                    nodes.forEach(node => {
                        if (node.id > maxId) maxId = node.id;
                        if (node.children) findMaxId(node.children);
                    });
                };
                
                findMaxId(this.treeData);
                this.nextId = maxId + 1;
            }
            
            // 初始化默认树
            initializeDefaultTree() {
                this.treeData = [
                    new TreeNode(this.nextId++, '项目文档', [
                        new TreeNode(this.nextId++, '需求文档'),
                        new TreeNode(this.nextId++, '设计文档', [])
                    ]),
                    new TreeNode(this.nextId++, '开发计划', [
                        new TreeNode(this.nextId++, '前端开发', []),
                        new TreeNode(this.nextId++, '后端开发'),
                        new TreeNode(this.nextId++, '测试计划', [])
                    ]),
                    new TreeNode(this.nextId++, '会议记录', [])
                ];
            }
            
            // 更新数据状态
            updateDataStatus() {
                let nodeCount = 0;
                let folderCount = 0;
                
                const traverse = (nodes) => {
                    nodes.forEach(node => {
                        nodeCount++;
                        if (!node.isLeaf()) folderCount++;
                        if (node.children && node.children.length > 0) {
                            traverse(node.children);
                        }
                    });
                };
                
                traverse(this.treeData);
                document.getElementById('nodeCount').textContent = nodeCount;
                document.getElementById('folderCount').textContent = folderCount;
            }
            
            // 渲染树
            render() {
                this.container.innerHTML = '';
                this.treeData.forEach(node => this.renderNode(node, this.container));
                this.updateSelectedNodeInfo();
            }
            
            // 渲染节点
            renderNode(node, parentElement) {
                const li = document.createElement('li');
                li.className = 'tree-node';
                li.dataset.id = node.id;
                
                const nodeContent = document.createElement('div');
                nodeContent.className = `node-content ${node.selected ? 'selected' : ''}`;
                
                // 节点图标（文件夹或文件）
                const nodeIcon = document.createElement('div');
                nodeIcon.className = 'node-icon';
                
               if (node.expanded) {
                    nodeIcon.innerHTML = '<i class="fas fa-folder-open folder-icon"></i>';
                    nodeIcon.className += ' folder-icon';
                } else {
                    nodeIcon.innerHTML = '<i class="fas fa-folder folder-icon"></i>';
                    nodeIcon.className += ' folder-icon';
                }
                
                const label = document.createElement('div');
                label.className = 'node-label';
                label.textContent = node.name;
                
                nodeContent.appendChild(nodeIcon);
                nodeContent.appendChild(label);
                
                li.appendChild(nodeContent);
                
                const childrenContainer = document.createElement('ul');
                childrenContainer.className = 'children';
                
                if (node.expanded) {
                    childrenContainer.style.display = 'block';
                    
                    if (node.children.length > 0) {
                        node.children.forEach(child => this.renderNode(child, childrenContainer));
                    } else if (!node.loaded) {
                        // 显示加载中
                        const loading = document.createElement('div');
                        loading.className = 'loading';
                        loading.innerHTML = '<div class="spinner"></div> 正在异步加载子节点...';
                        childrenContainer.appendChild(loading);
                        
                        // 模拟异步加载
                        this.loadChildren(node, childrenContainer);
                    }
                }
                
                li.appendChild(childrenContainer);
                parentElement.appendChild(li);
                
                // 设置展开状态类
                if (node.expanded) {
                    li.classList.add('expanded');
                }
            }
            
            // 模拟异步加载子节点
            loadChildren(node, container) {
                // 模拟API请求延迟
                setTimeout(() => {
                    // 模拟API数据
                    const newChildren = [
                        new TreeNode(this.nextId++, `${node.name}-子节点1`),
                        new TreeNode(this.nextId++, `${node.name}-子节点2`, []),
                        new TreeNode(this.nextId++, `${node.name}-子节点3`)
                    ];
                    
                    node.children = newChildren;
                    node.loaded = true;
                    
                    // 移除加载指示器
                    container.innerHTML = '';
                    
                    // 渲染新节点
                    newChildren.forEach(child => this.renderNode(child, container));
                    
                    this.saveTreeState();
                }, 1500);
            }
            
            // 添加节点
            addNode(parentId, name) {
                const newNode = new TreeNode(this.nextId++, name);
                
                if (!parentId) {
                    // 添加根节点
                    this.treeData.push(newNode);
                } else {
                    // 查找父节点
                    const parentNode = this.findNode(parentId, this.treeData);
                    if (parentNode) {
                        parentNode.children.push(newNode);
                        // 自动展开父节点
                        if (!parentNode.expanded) {
                            parentNode.expanded = true;
                        }
                    }
                }
                
                this.saveTreeState();
                this.render();
            }
            
            // 编辑节点
            editNode(nodeId, newName) {
                const node = this.findNode(nodeId, this.treeData);
                if (node) {
                    node.name = newName;
                    this.saveTreeState();
                    this.render();
                }
            }
            
            // 删除节点
            deleteNode(nodeId) {
                const deleteRecursive = (nodes, id) => {
                    for (let i = 0; i < nodes.length; i++) {
                        if (nodes[i].id === id) {
                            // 如果删除的是选中的节点，清除选中状态
                            if (this.selectedNode && this.selectedNode.id === id) {
                                this.selectedNode = null;
                                this.updateToolbarState();
                            }
                            nodes.splice(i, 1);
                            return true;
                        }
                        
                        if (nodes[i].children && nodes[i].children.length > 0) {
                            if (deleteRecursive(nodes[i].children, id)) {
                                return true;
                            }
                        }
                    }
                    return false;
                };
                
                deleteRecursive(this.treeData, nodeId);
                this.saveTreeState();
                this.render();
            }
            
            // 查找节点
            findNode(id, nodes) {
                for (const node of nodes) {
                    if (node.id === id) {
                        return node;
                    }
                    
                    if (node.children && node.children.length > 0) {
                        const found = this.findNode(id, node.children);
                        if (found) return found;
                    }
                }
                return null;
            }
            
            // 切换节点展开状态
            toggleNode(nodeId) {
                const node = this.findNode(nodeId, this.treeData);
                if (node && !node.isLeaf()) {
                    node.expanded = !node.expanded;
                    this.saveTreeState();
                    this.render();
                }
            }
            
            // 展开所有节点
            expandAll() {
                const expand = (nodes) => {
                    nodes.forEach(node => {
                        node.expanded = true;
                        if (node.children && node.children.length > 0) {
                            expand(node.children);
                        }
                    });
                };
                
                expand(this.treeData);
                this.saveTreeState();
                this.render();
            }
            
            // 折叠所有节点
            collapseAll() {
                const collapse = (nodes) => {
                    nodes.forEach(node => {
                        node.expanded = false;
                        if (node.children && node.children.length > 0) {
                            collapse(node.children);
                        }
                    });
                };
                
                collapse(this.treeData);
                this.saveTreeState();
                this.render();
            }
            
            // 选中节点
            selectNode(nodeId) {
                // 清除之前选中的节点
                this.clearSelection(this.treeData);
                
                // 设置新选中的节点
                const node = this.findNode(nodeId, this.treeData);
                if (node) {
                    node.selected = true;
                    this.selectedNode = node;
                    this.updateToolbarState();
                    this.render();
                }
            }
            
            // 清除所有节点的选中状态
            clearSelection(nodes) {
                nodes.forEach(node => {
                    node.selected = false;
                    if (node.children && node.children.length > 0) {
                        this.clearSelection(node.children);
                    }
                });
            }
            
            // 更新选中节点信息
            updateSelectedNodeInfo() {
                const infoContainer = document.getElementById('selectedNodeInfo');
                
                if (this.selectedNode) {
                    infoContainer.innerHTML = `
                        <div class="info-item">
                            <i class="fas fa-id-card"></i>
                            <span class="info-content">ID: ${this.selectedNode.id}</span>
                        </div>
                        <div class="info-item">
                            <i class="fas fa-tag"></i>
                            <span class="info-content">名称: ${this.selectedNode.name}</span>
                        </div>
                        <div class="info-item">
                            <i class="fas fa-folder"></i>
                            <span class="info-content">类型: ${this.selectedNode.isLeaf() ? '文件' : '文件夹'}</span>
                        </div>
                        <div class="info-item">
                            <i class="fas fa-folder-open"></i>
                            <span class="info-content">状态: ${this.selectedNode.expanded ? '已展开' : '已折叠'}</span>
                        </div>
                    `;
                } else {
                    infoContainer.innerHTML = `
                        <div class="no-selection">
                            <i class="fas fa-info-circle"></i> 请从左侧树中选择一个节点
                        </div>
                    `;
                }
            }
            
            // 更新工具栏状态
            updateToolbarState() {
                const hasSelection = this.selectedNode !== null;
                document.getElementById('addChildBtn').disabled = !hasSelection;
                document.getElementById('editNodeBtn').disabled = !hasSelection;
                document.getElementById('deleteNodeBtn').disabled = !hasSelection;
            }
            
            // 设置键盘导航
            setupKeyboardNavigation() {
                document.addEventListener('keydown', (e) => {
                    if (e.key === 'ArrowDown') {
                        this.navigateToNextNode();
                        e.preventDefault();
                    } else if (e.key === 'ArrowUp') {
                        this.navigateToPrevNode();
                        e.preventDefault();
                    } else if (e.key === '+' && this.selectedNode) {
                        document.getElementById('addChildBtn').click();
                    } else if (e.key === 'e' && this.selectedNode) {
                        document.getElementById('editNodeBtn').click();
                    } else if (e.key === 'Delete' && this.selectedNode) {
                        document.getElementById('deleteNodeBtn').click();
                    }
                });
            }
            
            // 导航到下一个节点
            navigateToNextNode() {
                const allNodes = this.getAllNodes();
                const currentIndex = this.selectedNode 
                    ? allNodes.findIndex(node => node.id === this.selectedNode.id) 
                    : -1;
                
                const nextIndex = (currentIndex + 1) % allNodes.length;
                this.selectNode(allNodes[nextIndex].id);
            }
            
            // 导航到上一个节点
            navigateToPrevNode() {
                const allNodes = this.getAllNodes();
                const currentIndex = this.selectedNode 
                    ? allNodes.findIndex(node => node.id === this.selectedNode.id) 
                    : 0;
                
                const prevIndex = (currentIndex - 1 + allNodes.length) % allNodes.length;
                this.selectNode(allNodes[prevIndex].id);
            }
            
            // 获取所有节点（扁平化）
            getAllNodes() {
                const nodes = [];
                
                const traverse = (nodeList) => {
                    nodeList.forEach(node => {
                        nodes.push(node);
                        if (node.children && node.children.length > 0) {
                            traverse(node.children);
                        }
                    });
                };
                
                traverse(this.treeData);
                return nodes;
            }
            
            // 设置事件监听器
            setupEventListeners() {
                // 节点点击事件
                this.container.addEventListener('click', (e) => {
                    const target = e.target;
                    const nodeElement = target.closest('.tree-node');
                    
                    if (!nodeElement) return;
                    
                    const nodeId = parseInt(nodeElement.dataset.id);
                    
                    // 点击图标 - 展开/折叠
                    if (target.closest('.node-icon')) {
                        this.toggleNode(nodeId);
                        return;
                    }
                    
                    // 节点标签点击 - 选中节点
                    if (target.closest('.node-label') || 
                        target.closest('.node-content')) {
                        this.selectNode(nodeId);
                    }
                });
                
                // 添加根节点按钮
                document.getElementById('addRootBtn').addEventListener('click', () => {
                    const name = prompt('请输入根节点名称:', '新节点');
                    if (name) {
                        this.addNode(null, name);
                    }
                });
                
                // 添加子节点按钮
                document.getElementById('addChildBtn').addEventListener('click', () => {
                    if (!this.selectedNode) return;
                    
                    const name = prompt('请输入子节点名称:', '新子节点');
                    if (name) {
                        this.addNode(this.selectedNode.id, name);
                    }
                });
                
                // 编辑节点按钮
                document.getElementById('editNodeBtn').addEventListener('click', () => {
                    if (!this.selectedNode) return;
                    
                    const newName = prompt('编辑节点名称:', this.selectedNode.name);
                    if (newName !== null) {
                        this.editNode(this.selectedNode.id, newName);
                    }
                });
                
                // 删除节点按钮
                document.getElementById('deleteNodeBtn').addEventListener('click', () => {
                    if (!this.selectedNode) return;
                    
                    if (confirm(`确定要删除节点 "${this.selectedNode.name}" 及其所有子节点吗？`)) {
                        this.deleteNode(this.selectedNode.id);
                    }
                });
                
                // 全部展开按钮
                document.getElementById('expandAllBtn').addEventListener('click', () => {
                    this.expandAll();
                });
                
                // 全部折叠按钮
                document.getElementById('collapseAllBtn').addEventListener('click', () => {
                    this.collapseAll();
                });
                
                // 重置按钮
                document.getElementById('resetBtn').addEventListener('click', () => {
                    if (confirm('确定要重置为默认树结构吗？当前数据将丢失。')) {
                        localStorage.removeItem('treeData');
                        this.nextId = 1;
                        this.selectedNode = null;
                        this.updateToolbarState();
                        this.initializeDefaultTree();
                        this.saveTreeState();
                        this.render();
                    }
                });
            }
        }

        // 初始化树形菜单
        document.addEventListener('DOMContentLoaded', () => {
            const treeMenu = new TreeMenu('treeMenu');
        });
    </script>
</body>
</html>